package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemSelfMemberPicBinding;
import com.sgb.kjwl.databinding.ItemShopTeamPicBinding;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamAlbumAdapter;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.io.Serializable;
import java.util.List;

public class ShopTeamPicAdapter extends BaseRecycleAdapter<String> {

    private boolean mIsVisible; // 是否显示遮罩

    private GridLayoutManager mGridLayoutManager;

    public ShopTeamPicAdapter(Context context, List<String> datas, GridLayoutManager mGridLayoutManager) {
        super(context, R.layout.item_shop_team_pic, datas);
        mIsVisible = getDatas().size() > 4;
        this.mGridLayoutManager = mGridLayoutManager;
    }

    @Override
    public int getItemCount() {
        return mIsVisible ? Math.min(getDatas().size(), 4) :  super.getItemCount();
    }

    @Override
    public void convert(ItemViewHolder holder, final String entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView view1 = holder.itemView.findViewById(R.id.iv_view);
        RequestOptions options = new RequestOptions();
        options.override(400, 400);
        Glide.with( holder.getmBinding().getRoot().getContext()).load(entity).apply(options).into(view1);
        //((ItemShopTeamPicBinding) holder.getmBinding()).setSize(getItemCount());
        if (mIsVisible) {
            if (holder.getAdapterPosition() == 3) {
                ((ItemShopTeamPicBinding) holder.getmBinding()).llCount.setVisibility(View.VISIBLE);
                holder.getmBinding().getRoot().setOnClickListener(view -> {
                    if (mIsVisible) {
                        mIsVisible = false;
                        notifyDataSetChanged();
                    }
                });
            } else {
                holder.getmBinding().getRoot().setVisibility(View.VISIBLE);
                ((ItemShopTeamPicBinding) holder.getmBinding()).llCount.setVisibility(View.GONE);
                holder.getmBinding().getRoot().setOnClickListener(view -> {
                    Intent intent = new Intent(holder.getmBinding().getRoot().getContext(), PhotoViewActivity.class);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) getDatas());
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, holder.getAdapterPosition());
                    holder.getmBinding().getRoot().getContext().startActivity(intent);
                });
            }
        } else {
            holder.getmBinding().getRoot().setVisibility(View.VISIBLE);
            ((ItemShopTeamPicBinding) holder.getmBinding()).llCount.setVisibility(View.GONE);
            holder.getmBinding().getRoot().setOnClickListener(view -> {
                Intent intent = new Intent(holder.getmBinding().getRoot().getContext(), PhotoViewActivity.class);
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) getDatas());
                intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, holder.getAdapterPosition());
                holder.getmBinding().getRoot().getContext().startActivity(intent);
            });
        }
    }
}
